﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CoreCommon;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace gte
{
    public partial class FormCard : Form
    {
        #region Properties

        public DataTable DT { get { return _dt; } }

        #endregion

        public static DialogResult Item(int id)
        {
            FormCard frm = new FormCard(id);
            return frm.ShowDialog();
        }
        
        private FormCard(int id)
        {
            InitializeComponent();
            _id = id;
        }
        private void FormCard_KeyDown(object sender, KeyEventArgs e)
        {
            if (!btnSave.Focused)
                CloseForm(e);
        }
        private void CloseForm(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.DialogResult = DialogResult.Cancel;
                    break;
                case Keys.Return:
                    CheckFormsValue();
                    SQL_Fetch sf = new SQL_Fetch();
                    sf.Name = tbName.Text;
                    sf.PathE = tbPathE.Text;
                    sf.PathF = tbPathF.Text;
                    sf.SqlText = tbSelect.Text;
                    sf.ID = _id;

                    StringBuilder sb = new StringBuilder();
                    using (XmlWriter xtw = XmlTextWriter.Create(sb))
                    {
                        xtw.WriteStartDocument();
                        xtw.WriteStartElement("rows");
                        foreach (DataGridViewRow dr in dgvComparer.Rows)
                        {
                            xtw.WriteStartElement("row");
                            xtw.WriteAttributeString(dgvComparer.Columns[0].Name, dr.Cells[0].Value.ToString());
                            xtw.WriteAttributeString(dgvComparer.Columns[1].Name, dr.Cells[1].Value.ToString());
                            xtw.WriteAttributeString(dgvComparer.Columns[2].Name, dr.Cells[2].Value.ToString());
                            xtw.WriteAttributeString(dgvComparer.Columns[3].Name, dr.Cells[3].Value.ToString());
                            xtw.WriteEndElement();
                        }
                        xtw.WriteEndElement();
                        xtw.WriteEndDocument();
                    }
                    sf.Comparer = sb.ToString();

                    if (_id == 0)
                        sf.SaveToDb();
                    else
                        sf.EditRowDb();
                    
                    this.DialogResult = DialogResult.OK;
                    break;
            }
        }
        private void CheckFormsValue()
        {
            string[] strs = new string[]{
                tbName.Text,
                tbPathE.Text,
                tbPathF.Text,
                tbSelect.Text
            };
            if (Routins.StrIsEmpty(strs))
                throw new CoreException(ErrType.Message, ErrorMsg.NotAllFieldsCorrect);
            
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                CloseForm(new KeyEventArgs(Keys.Return));
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
            
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                
                ORA_Fetch.ExecSelect(DT, "id", tbSelect.Text);

                dgvComparer.Rows.Clear();
                foreach (DataColumn item in DT.Columns)
                    dgvComparer.Rows.Add(item.ColumnName, "Лист 1", "1", "A");
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private DataTable _dt = new DataTable();
        private int _id;

        private void FormCard_Load(object sender, EventArgs e)
        {
            if (_id != 0)
            {
                SQL_Fetch sf = SQL_Fetch.Load(_id);
                tbName.Text = sf.Name;
                tbSelect.Text = sf.SqlText;
                tbPathE.Text = sf.PathE;
                tbPathF.Text = sf.PathF;

                dgvComparer.Rows.Clear();
                XDocument xDoc = XDocument.Parse(sf.Comparer);
                foreach (XElement xe in xDoc.Descendants().Where(n => n.Name == "row"))
                {
                    dgvComparer.Rows.Add(
                        xe.Attribute(dgvComparer.Columns[0].Name).Value,
                        xe.Attribute(dgvComparer.Columns[1].Name).Value,
                        xe.Attribute(dgvComparer.Columns[2].Name).Value,
                        xe.Attribute(dgvComparer.Columns[3].Name).Value
                        );
                }
            }
        }

        
    }
}
